#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int N, E;
int vertMat[15][15];
bool visited[15];
enum
{
    unvisited,
    isvisited,
};
void dfs(int x)
{
    visited[x] = isvisited;
    cout << x << " ";
    for (int i = 0; i < N; ++i)
    {
        if (visited[i] == unvisited && vertMat[x][i] == 1)
            dfs(i);
    }
}
void bfs(int x)
{
    queue<int> que;
    que.push(x);
    while (!que.empty())
    {
        int node = que.front();
        que.pop();
        visited[node] = isvisited;
        cout << node << " ";
        for (int i = 0; i < N; ++i)
        {
            if (visited[i] == unvisited && vertMat[node][i] == 1)
            {
                visited[i] = isvisited;
                que.push(i);
            }
        }
    }
}
int main()
{
    cin >> N >> E;
    for (int i = 0, frm, to; i < E; ++i)
    {
        cin >> frm >> to;
        vertMat[frm][to] = 1;
        vertMat[to][frm] = 1;
    }
    //开始DFS的连通集
    for (int i = 0; i < N; ++i)
    {
        if (visited[i] == unvisited)
        {
            cout << "{ ";
            dfs(i);
            cout << "}" << endl;
        }
    }
    for (int i = 0; i < N; ++i)
        visited[i] = unvisited;
    //开始BFS的连通集
    for (int i = 0; i < N; ++i)
    {
        if (visited[i] == unvisited)
        {
            cout << "{ ";
            bfs(i);
            cout << "}" << endl;
        }
    }
    return 0;
}